package com.sicau.socialpracticesystem.util;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

/**
 * @author sxt
 * @version 1.0
 * @description 逆地址编码工具 基于高德地图web-api
 * @date 2021/4/28 7:02 下午
 */
public class InverseGeoUtil {

    /**
     * 逆地址编码请求url
     */
    private static final String inverseUrl = "https://restapi.amap.com/v3/geocode/geo";

    /**
     * 高德Key
     */
    private static final String key = "97bc4f5d88c414ef6d5d273d74160356";

    public static LatLng inverse(String address) {
        String result = OkHttpUtils.httpGet(inverseUrl + "?key=" + key + "&address=" + address);
        JSONObject parseObject = JSONObject.parseObject(result);
        String[] latLng = parseObject.getJSONArray("geocodes").getJSONObject(0).getString("location").split(",");
        return new LatLng(Double.parseDouble(latLng[1]), Double.parseDouble(latLng[0]));
    }

    @Data
    @ToString
    @AllArgsConstructor
    public static class LatLng {
        /**
         * 纬度
         */
        private Double latitude;

        /**
         * 经度
         */
        private Double longitude;
    }

//    public static void main(String[] args) {
//        System.out.println(inverse("四川农业大学成都校区"));
//    }
}
